const collectModel = require('../model/collectModel.js');

const collectController = {
    getCollectList: async  (req,res)=>{
        try {
            const {list,total} = await  collectModel.getCollectList(req.body);
            if(list.length > 0){
                res.send({
                    code:1,
                    msg:'获取用户收藏列表成功',
                    list,
                    total,

                })
            } else{
                res.send({
                    code:0,
                    msg:'获取用户收藏列表出错了,请填入正确的参数',
                })
            }
        }catch (error) {
            console.log(error,'获取用户收藏列表controller层出错了');
        }

    },
    isCollect: async (req,res) => {
        try {
            const result = await  collectModel.isCollect(req.body);
            if(result === 1){
                res.send({
                    code:1,
                    msg:'用户已收藏',
                    state:1
                })
            } else  if(result===0){
                res.send({
                    code:2,
                    msg:'用户未收藏',
                    state:2
                })
            }else{
                res.send({
                    code:0,
                    msg:'服务器出错了',
                })
            }
        }catch (error) {
            console.log(error,'添加收藏controller层出错了');
        }
    },
    addCollect: async (req,res) => {
        try {
            const result = await  collectModel.addCollect(req.body);
            if(result === 1){
                res.send({
                    code:1,
                    msg:'添加收藏成功',
                    state:1
                })
            } else  if(result===0){
                res.send({
                    code:2,
                    msg:'取消收藏',
                    state:2
                })
            }else{
                res.send({
                    code:0,
                    msg:'服务器出错了',
                })
            }
        }catch (error) {
            console.log(error,'添加收藏controller层出错了');
        }
    },
    deleteCollect: async (req,res) => {
        try {
            const result = await  collectModel.deleteCollect(req.params);
            if(result === 1){
                res.send({
                    code:1,
                    msg:'删除收藏成功'
                })
            } else  if(result===0){
                res.send({
                    code:2,
                    msg:'删除收藏失败'
                })
            }else{
                res.send({
                    code:0,
                    msg:'服务器出错了',
                })
            }
        }catch (error) {
            console.log(error,'删除收藏controller层出错了');
        }
    },
};

module.exports = collectController;